<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>API测试</title>
  <!-- 尝试多个CDN源，增加加载成功率 -->
  <script>
    // 检查axios是否已定义的函数
    function isAxiosLoaded() {
      return typeof axios !== 'undefined';
    }
    
    // 尝试从不同的CDN加载axios
    function loadAxios(cdnUrls, currentIndex) {
      if (currentIndex >= cdnUrls.length) {
        document.getElementById('status').innerHTML = 
          '<span style="color:red">无法加载axios库，请检查网络连接</span>';
        return;
      }
      
      var script = document.createElement('script');
      script.src = cdnUrls[currentIndex];
      script.onload = function() {
        document.getElementById('status').innerHTML = 
          '<span style="color:green">axios加载成功！</span>';
        document.getElementById('testBtn').disabled = false;
      };
      script.onerror = function() {
        document.getElementById('status').innerHTML = 
          '<span>尝试加载备用源...</span>';
        loadAxios(cdnUrls, currentIndex + 1);
      };
      document.head.appendChild(script);
    }
    
    // 定义axios的替代方法，以防库未加载
    window.testApiConnection = function() {
      const resultDiv = document.getElementById('result');
      
      if (!isAxiosLoaded()) {
        resultDiv.innerHTML = '<div style="color: red;">axios库未加载，无法测试API连接</div>';
        return;
      }
      
      resultDiv.innerHTML = '测试中...';
      
      // 尝试直接请求
      makeRequest('/api/teaching/test-connection', '代理请求')
        .catch(() => {
          // 如果代理请求失败，尝试直接URL
          return makeRequest('http://localhost:8080/api/teaching/test-connection', '直接URL');
        })
        .catch(finalError => {
          resultDiv.innerHTML += `
            <div style="color: red; margin-top: 10px;">
              所有连接方式都失败！<br>
              请确认：<br>
              1. 后端服务运行在8080端口<br>
              2. 前端代理配置正确<br>
              3. 后端CORS配置正确
            </div>
          `;
        });
    };
    
    function makeRequest(url, requestType) {
      const resultDiv = document.getElementById('result');
      
      return axios.get(url)
        .then(response => {
          resultDiv.innerHTML += `
            <div style="color: green; margin-top: 10px;">
              ${requestType}连接成功！<br>
              状态: ${response.status}<br>
              数据: ${JSON.stringify(response.data, null, 2)}
            </div>
          `;
          return response;
        })
        .catch(error => {
          resultDiv.innerHTML += `
            <div style="color: red; margin-top: 10px;">
              ${requestType}连接失败！<br>
              错误信息: ${error.message}<br>
              ${error.response ? ('状态: ' + error.response.status) : '无响应'}
            </div>
          `;
          throw error;
        });
    }
  </script>
  <style>
    body {
      font-family: Arial, sans-serif;
      max-width: 800px;
      margin: 0 auto;
      padding: 20px;
    }
    .card {
      border: 1px solid #ddd;
      border-radius: 8px;
      padding: 16px;
      margin-bottom: 16px;
      box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    }
    button {
      background-color: #4CAF50;
      border: none;
      color: white;
      padding: 10px 20px;
      text-align: center;
      text-decoration: none;
      display: inline-block;
      font-size: 16px;
      margin: 4px 2px;
      cursor: pointer;
      border-radius: 4px;
    }
    button:disabled {
      background-color: #cccccc;
      cursor: not-allowed;
    }
    #result {
      margin-top: 20px;
      padding: 10px;
      border: 1px solid #ccc;
      border-radius: 4px;
      background-color: #f9f9f9;
      white-space: pre-wrap;
    }
  </style>
</head>
<body>
  <div class="card">
    <h1>API连接测试</h1>
    <div id="status">正在加载axios库...</div>
    <button id="testBtn" onclick="testApiConnection()" disabled>测试API连接</button>
    <div id="result"></div>
  </div>
  
  <div class="card">
    <h3>调试信息</h3>
    <ul>
      <li><strong>前端URL:</strong> <script>document.write(window.location.href)</script></li>
      <li><strong>预期后端URL:</strong> http://localhost:8080</li>
      <li><strong>测试连接点:</strong> /api/teaching/test-connection</li>
    </ul>
  </div>

  <script>
    // 加载axios（尝试多个源）
    loadAxios([
      'https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js',
      'https://unpkg.com/axios/dist/axios.min.js',
      'https://cdnjs.cloudflare.com/ajax/libs/axios/1.5.0/axios.min.js'
    ], 0);
  </script>
</body>
</html> 